package com.shine.minepressure.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shine.minepressure.BR;
import com.shine.minepressure.R;
import com.shine.minepressure.app.GlideShapeImageLoader;
import com.shine.minepressure.base.BaseFragment;
import com.shine.minepressure.bean.SensorNumberMessageBean;
import com.shine.minepressure.bean.SensorStateMessageBean;
import com.shine.minepressure.databinding.FragmentHomeBinding;
import com.shine.minepressure.http.ApiException;
import com.shine.minepressure.http.ECObserver;
import com.shine.minepressure.http.RetrofitClient;
import com.shine.minepressure.http.RxUtils;
import com.shine.minepressure.http.Service;
import com.shine.minepressure.ui.chart.ColumnChartFragment;
import com.shine.minepressure.ui.chart.PieChartFragment;
import com.shine.minepressure.ui.fragment.viewModel.HomeViewModel;
import com.shine.minepressure.ui.history.HistoryDataActivity;
import com.shine.minepressure.ui.realTime.RealTimeDataActivity;
import com.shine.minepressure.utils.ScreenUtils;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public HomeViewModel initViewModel() {
        return new HomeViewModel(getActivity());
    }

    @Override
    public void initData() {
        super.initData();

        View view = binding.statusBar;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.height = ScreenUtils.getStatusBarHeight(view.getContext());
            view.setLayoutParams(params);
        }


        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.refreshLayout.finishRefresh(1000);

                getSensorStateMessage();
                getSensorNumberMessage();
            }
        });
        binding.refreshLayout.setEnableLoadMore(false);


        binding.advertBanner.setImageLoader(new GlideShapeImageLoader());
        List<Integer> imgList = new ArrayList<>();
        imgList.add(R.mipmap.banner_img1);
        binding.advertBanner.setImages(imgList);
        binding.advertBanner.start();

        binding.advertBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });

        binding.video.setOnClickListener(this);
        binding.search.setOnClickListener(this);
        binding.resultInput.setOnClickListener(this);


        binding.message.setMarqueeRepeatLimit(Integer.MAX_VALUE);
        binding.message.setFocusable(true);
        binding.message.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        binding.message.setSingleLine();
        binding.message.setFocusableInTouchMode(true);
        binding.message.setHorizontallyScrolling(true);

        getSensorStateMessage();
        getSensorNumberMessage();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search:
                switchActivity(RealTimeDataActivity.class);
                break;
            case R.id.resultInput:
                switchActivity(HistoryDataActivity.class);
                break;
            case R.id.video:
                break;
            default:
                break;
        }

    }

    /**
     * 传感状态比例信息
     */
    private void getSensorStateMessage() {
        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .getSensorStateMessage()
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<SensorStateMessageBean>() {
                    @Override
                    protected void _onNext(SensorStateMessageBean sensorStateMessageBean) {
//                        dismissLoading();
                        binding.pieChartLayout.removeAllViews();
                        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.pieChartLayout, new PieChartFragment(sensorStateMessageBean)).commit();

                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }
    /**
     * 传感器数量信息
     */
    private void getSensorNumberMessage() {
//        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .getSensorNumberMessage()
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<SensorNumberMessageBean>() {
                    @Override
                    protected void _onNext(SensorNumberMessageBean sensorNumberMessageBean) {
                        dismissLoading();
                        binding.lineChartLayout.removeAllViews();
                        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.lineChartLayout, new ColumnChartFragment(sensorNumberMessageBean)).commit();
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }



}